$(function () {
    InitialPage();
    GetGrid();
});
//初始化页面
function InitialPage() {
    //resize重设布局;
    $(window).resize(function (e) {
        window.setTimeout(function () {
            $('#gridTable').setGridWidth(($('.gridPanel').width()));
            $('#gridTable').setGridHeight($(window).height() - 108.5);
        }, 200);
        e.stopPropagation();
    });
}
//加载表格
function GetGrid() {
    var selectedRowIndex = 0;
    var $gridTable = $('#gridTable');
    $gridTable.jqGrid({
        autowidth: true,
        height: $(window).height() - 108.5,
        url: "../../CustomerManage/MACustomer/GetPageListJson",
        datatype: "json",
        colModel: [
            { label: '主键', name: 'Id', hidden: true },
            { label: '会员编号', name: 'EnCode', index: 'EnCode', width: 100, align: 'left', sortable: true },
            { label: '会员名字', name: 'Name', index: 'Name', width: 100, align: 'left', sortable: true },
            { label: '手机号', name: 'Mobile', index: 'Mobile', width: 100, align: 'left', sortable: true },
            { label: '剩余短信（条）', name: 'Number', index: 'Number', width: 100, align: 'left', sortable: true },
            { label: '创建日期', name: 'CreateDate', index: 'CreateDate', width: 100, align: 'left', sortable: true },
            {
                label: "禁止登陆", name: "IsDeleted", index: "IsDeleted", width: 20, align: "center",
                formatter: function (cellvalue) {
                    return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                }
            },
        ],
        viewrecords: true,
        rowNum: 30,
        rowList: [30, 50, 100],
        pager: "#gridPager",
        sortname: 'CreateDate',
        sortorder: 'desc',
        rownumbers: true,
        shrinkToFit: false,
        gridview: true,
        onSelectRow: function () {
            selectedRowIndex = $('#' + this.id).getGridParam('selrow');
        },
        gridComplete: function () {
            $('#' + this.id).setSelection(selectedRowIndex, false);
        }
    });
    //查询条件
    $("#queryCondition .dropdown-menu li").click(function () {
        var text = $(this).find('a').html();
        var value = $(this).find('a').attr('data-value');
        $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
    });
    //查询事件
    $("#btn_Search").click(function () {
        var queryJson = {
            condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
            keyword: $("#txt_Keyword").val()
        }
        $gridTable.jqGrid('setGridParam', {
            postData: { queryJson: JSON.stringify(queryJson) },
            page: 1
        }).trigger('reloadGrid');
    });
    //查询回车
    $('#txt_Keyword').bind('keypress', function (event) {
        if (event.keyCode == "13") {
            $('#btn_Search').trigger("click");
        }
    });
}
//新增
function btn_add() {
    dialogOpen({
        id: 'Form',
        title: '添加会员管理类',
        url: '/CustomerManage/MACustomer/Form',
        width: '700px',
        height: '450px',
        callBack: function (iframeId) {
            top.frames[iframeId].AcceptClick();
        }
    });
}
//编辑
function btn_edit() {
    var keyValue = $("#gridTable").jqGridRowValue("Id");
    if (checkedRow(keyValue)) {
        dialogOpen({
            id: 'Form',
            title: '编辑会员管理类',
            url: '/CustomerManage/MACustomer/Form?keyValue=' + keyValue,
            width: '700px',
            height: '450px',
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        })
    }
}
//删除
function btn_delete() {
    var keyValue = $("#gridTable").jqGridRowValue('#gridTable', 'Id');
    if (keyValue) {
        $.RemoveForm({
            url: '../../CustomerManage/MACustomer/RemoveForm',
            param: { keyValue: keyValue },
            success: function (data) {
                $('#gridTable').trigger('reloadGrid');
            }
        })
    } else {
        dialogMsg('请选择需要删除的会员管理类！', 0);
    }
}